package org.kineticsproject.application.util;

import java.io.File;
import java.io.IOException;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;

import data.DataFactory;
import data.Device;
import data.Devices;
import data.Sensor;
import data.Units;

public class PopulateDevices {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		
		Devices devices = DataFactory.eINSTANCE.createDevices();
		
		Sensor      s  = DataFactory.eINSTANCE.createSensor(); 
		s.setUnits(Units.METERS);
		s.setName("Altimeter");
		s.setSamplePeriod(1);
		
		Sensor		s1 = DataFactory.eINSTANCE.createSensor();
		s1.setUnits(Units.METERS);
		s1.setName("GPS");
		s1.setSamplePeriod(1);

		Sensor		s2 = DataFactory.eINSTANCE.createSensor();
		s2.setUnits(Units.DEGREES);
		s2.setName("Compass");
		s2.setSamplePeriod(2);
		

		Sensor		s3 = DataFactory.eINSTANCE.createSensor();
		s3.setUnits(Units.METERS);
		s3.setName("GPS");
		s3.setSamplePeriod(1);
		
		Sensor		s4 = DataFactory.eINSTANCE.createSensor();
		s4.setUnits(Units.DEGREES);
		s4.setName("Compass");
		s4.setSamplePeriod(2);
		
		Sensor		s5 = DataFactory.eINSTANCE.createSensor();
		s5.setUnits(Units.METERS);
		s5.setName("Altimeter");
		s5.setSamplePeriod(10);
		
		Sensor		s6 = DataFactory.eINSTANCE.createSensor();
		s6.setUnits(Units.BEATSPERMINUTE);
		s6.setName("HeartRate");
		s6.setSamplePeriod(2);
		
		Sensor		s7 = DataFactory.eINSTANCE.createSensor();
		s7.setUnits(Units.METERS);
		s7.setName("GPS");
		s7.setSamplePeriod(1);
		
		
		Device		d = DataFactory.eINSTANCE.createDevice();
		d.setManufacturer("Garmin");
		d.setModel("GPSMap60CS");
		d.getSensors().add(s);
		d.getSensors().add(s1);
		d.getSensors().add(s2);
		devices.getDevices().add(d);
		
		Device 		d1 = DataFactory.eINSTANCE.createDevice();
		d1.setManufacturer("Garmin");
		d1.setModel("Etrex");
		d1.getSensors().add(s7);
		devices.getDevices().add(d1);

		Device		d2 = DataFactory.eINSTANCE.createDevice();
		d2.getSensors().add(s6);
		d2.getSensors().add(s5);
		d2.setManufacturer("Polar");
		d2.setModel("AXN700");
		devices.getDevices().add(d2);


		Device		d3 = DataFactory.eINSTANCE.createDevice();
		d3.getSensors().add(s4);
		d3.getSensors().add(s3);
		devices.getDevices().add(d3);
		
		//Create new ResourceSet & Resource
		ResourceSet resourceSet = new ResourceSetImpl();
		
		// Register XML resource factory
		resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", 
		new XMIResourceFactoryImpl());

		Resource resource = resourceSet.createResource(URI.createFileURI(new File("myDevices.xmi").getAbsolutePath()));

		// add the root object to the resource
		resource.getContents().add(devices);
		// serialize resource – you can specify also serialization
		// options which defined on org.eclipse.emf.ecore.xmi.XMIResource
		resource.save(null);
		


	}

}
